package com.yimixian.app.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yimixian.app.R;
import com.yimixian.app.activity.YMXMainActivity;

public class PaoWuXianAnimTool {

	public static Map<String, PaoWuXianAnimator> animMap = new HashMap<>();
	public static Handler handler = new Handler();
	public final static int DEFAULT_DURATION = 600;

	/**
	 * 开始抛物线动画
	 * @param context
	 * @param fromView 从哪个控件开始动画
	 * @param toView 到哪个控件结束动画
	 * @param animBitmap 动画控件显示的图片
	 * @param duration 动画持续时间
	 */
	public static void startAnim(final Context context, View fromView, View toView, Bitmap animBitmap, int duration){
		startAnim(context, fromView, toView, -1, animBitmap, duration, null);
	}

	/**
	 * 开始抛物线动画
	 * @param context
	 * @param fromView 从哪个控件开始动画
	 * @param toView 到哪个控件结束动画
	 * @param animBitmap 动画控件显示的图片
	 * @param yTopDistance 最高点距起点距离
	 * @param duration 动画持续时间
	 */
	public static void startAnim(final Context context, View fromView, View toView, Bitmap animBitmap, int yTopDistance, int duration){
		startAnim(context, fromView, toView, yTopDistance, animBitmap, duration, null);
	}

	/**
	 * 开始抛物线动画
	 * @param context
	 * @param fromView 从哪个控件开始动画
	 * @param toView 到哪个控件结束动画
	 * @param animBitmap 动画控件显示的图片
	 * @param duration 动画持续时间
	 * @param listener 动画结束监听
	 */
	public static void startAnim(final Context context, View fromView, View toView, Bitmap animBitmap, int duration, PaoWuXianAnimator.onPaoWuXianEndListener listener){
		startAnim(context, fromView, toView, -1, animBitmap, duration, listener);
	}

	/**
	 * 开始抛物线动画
	 * @param context
	 * @param fromView 从哪个控件开始动画
	 * @param toView 到哪个控件结束动画
	 * @param yTopDistance 最高点距起点距离
	 * @param animBitmap 动画控件显示的图片
	 * @param duration 动画持续时间
	 */
	public static void startAnim(final Context context, final View fromView, final View toView, final int yTopDistance,
								 final Bitmap animBitmap, final int duration, final PaoWuXianAnimator.onPaoWuXianEndListener listener){

		//4.0后支持此方法
		if(VERSION.SDK_INT < 14 || animBitmap == null){
			return;
		}

		handler.post(new Runnable() {
			@Override
			public void run() {

//				String key = fromView.hashCode() + "" + toView.hashCode();
//				if(animMap.containsKey(key)){
//					animMap.get(key).cancel();
//				}

				int yTdistance = yTopDistance;
				float fromViewWidth = fromView.getWidth();
				float fromViewHeight = fromView.getHeight();
				float toViewWidth = toView.getWidth();
				float toViewHeight = toView.getHeight();
				float animViewWidth = animBitmap.getWidth();
				float animViewHeight = animBitmap.getHeight();
				int parentTop = 0;
				FrameLayout activityView = (FrameLayout) ((Activity)context).findViewById(Window.ID_ANDROID_CONTENT);
//				View v_anim = activityView.findViewWithTag(key);
				View v_anim = null;
				int[] location = new int[2];
				activityView.getLocationOnScreen(location);
				parentTop = location[1];

				if(v_anim == null){
					v_anim = new View(context);
					activityView.addView(v_anim);
				}
//				v_anim.setTag(key);
				v_anim.setLayoutParams(new FrameLayout.LayoutParams((int) animViewWidth, (int) animViewHeight));
				v_anim.setBackgroundDrawable(new BitmapDrawable(animBitmap));

				int[] fromLocation = new int[2];
				fromView.getLocationOnScreen(fromLocation);
				int[] toLocation = new int[2];
				toView.getLocationOnScreen(toLocation);

				float animStartX = fromLocation[0] + (fromViewWidth - animViewWidth) / 2;
				float animStartY = fromLocation[1] - parentTop + (fromViewHeight - animViewHeight) / 2;
				float animEndX = toLocation[0] + (toViewWidth - animViewWidth) / 2;
				float animEndY = toLocation[1] - parentTop + (toViewHeight - animViewHeight) / 2;
				float xDistance = animEndX - animStartX;
				float yBottomDistance = animEndY - animStartY;

				//根据屏幕高分5段抛物线顶点高度
				if(yTdistance < 0){
					int duan = 5;
					int maxPaoWuXianHeight = 200;
					int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
					int eachHeight = (int) (screenHeight / duan);
					for (int i = 0; i < duan; i++) {
						if(animStartY < (i + 1) * eachHeight){
							yTdistance =  (i + 1) * maxPaoWuXianHeight / duan;
							break;
						}
					}
				}

				float yBdistance = yBottomDistance < 0 ? -yBottomDistance : yBottomDistance;

//				//设置动画最高点在屏幕顶端
//				yTopDistance = (int) animStartY;

				final float timeInSecond = (duration + 0f) / 1000;

				//计算竖直方向起始速度和加速度值
				float pA, pB, pC;
				if(yBottomDistance >= 0){
					pA = timeInSecond * timeInSecond;
					pB = 4 * timeInSecond * yTdistance;
					pC = -4 * yBdistance * yTdistance;
				}else{
					pA = timeInSecond * timeInSecond;
					pB = 4 * (yBdistance + yTdistance) * timeInSecond;
					pC = 4 * yBdistance * (yBdistance + yTdistance);
				}

				float v01 = (float) ((-pB + Math.sqrt(pB * pB - 4 * pA * pC)) / (2 * pA));
				float v02 = (float) ((-pB - Math.sqrt(pB * pB - 4 * pA * pC)) / (2 * pA));

				float v = 0, a = 0;

				if(v01 < 0 && v02 < 0){
					v = v01 < v02 ? v01 : v02;
				}else if(v01 < 0){
					v = v01;
				}else if(v02 < 0){
					v = v02;
				}

				if(yBottomDistance >= 0){
					a = 2 * (yBdistance - v * timeInSecond) / (timeInSecond * timeInSecond);
				}else{
					a = v * v / (2 * (yBdistance + yTdistance));
				}

				PaoWuXianAnimator pwxAnim = new PaoWuXianAnimator(v_anim, animStartX, animStartY, xDistance, duration, v, a);
				pwxAnim.setPaoWuXianEndListener(listener);
				pwxAnim.startAnim();
//				animMap.put(key, pwxAnim);
			}
		});


	}
	
	
}
